/**
 * Created by chenweicheng on 1/3/2017.
 */
/**
 * Created by Administrator on 2016/12/20 0020.
 */

import React, {Component} from 'react';
import {AppRegistry, View, Text, ScrollView, Image, StyleSheet, TouchableHighlight} from 'react-native';
import NetworkManager from '../NetworkManager';

export default class TestNetWorkManager extends Component {

    constructor(props) {
        super(props);
        this.signature = "";
    }

    handleLogin() {
        var url = 'http://snooker.quzitech.com/v1/user/login';
        var params = {'phone_number': '12312312345', 'password': '666666'};
        NetworkManager.post(url, params).then((response) => {
                console.log('Request success= ' + response);
                response = response.replace(/\\/g, "");
                console.log(response);
                let obj = JSON.parse(response);
                console.log("obj", obj);
                console.log("data", obj.data);
                console.log("signature", obj.data.signature);
                this.signature = obj.data.signature;
                console.log("userinfo", obj.data.UserInfo);

            },
            (error) => {
                console.log('Request fail = ' + error)
            });
    }

    handleget() {

        NetworkManager.get('http://snooker.quzitech.com/v1/user/info/signature=' + this.signature).then((response) => {
            //   let jsonObj = JSON.parse(JSON.parse(response));
            console.log(response);
        }, (error) => {
            console.log('error = ' + error);
        });
    }

    handlepatch() {


        NetworkManager.patch('http://snooker.quzitech.com/v1/user/info', {
            'gender': 1, 'signature': this.signature
        }).then((response) => {
            // let jsonObj = JSON.parse(JSON.parse(response));
            console.log('patch', response);
        }, (error) => {
            console.log('error = ' + error);
        });
    }

    handlerUploadWith(params) {
        NetworkManager.uploadWith('http://snooker.quzitech.com/v1/user/register', {
            'appkey': '18d3f1ac873e0',
            "phone_number": params.phone_number,
            "password": params.password,
            "name": params.name,
            "code": params.code
        }, "file:///storage/emulated/0/1488360505147.JPEG").then((response) => {
            console.log('Request success= ' + response);
            response = response.replace(/\\/g, "");
            console.log(response);
            let obj = JSON.parse(response);
            console.log("obj", obj);
            // let jsonObj = this.jsonParse(response);
            //
            // if (jsonObj.code == 0) {//register success
            //     this.login({
            //         "phone_number": params.phone_number,
            //         "password": params.password,
            //     }, (userinfo) => {
            //         callback(userinfo);
            //     });
            // } else {
            //     Alert.alert(JSON.parse(JSON.parse(response)).message, '', [{
            //         text: 'OK',
            //         onPress: () => console.log('OK Pressed!')
            //     }])
            // }
        }, (error) => {
            console.log('error = ' + error);
        });
    }

    render() {
        return (
            <View style={styles.container}>
                <TouchableHighlight style={{padding: 20, backgroundColor: '#ccc'}}
                                    onPress={() =>this.handleLogin()  }>
                    <Text>LOGIN_POST</Text>
                </TouchableHighlight>
                <TouchableHighlight style={{padding: 20, backgroundColor: '#ccc'}}
                                    onPress={() =>this.handleget()  }>
                    <Text>GET</Text>
                </TouchableHighlight>
                <TouchableHighlight style={{padding: 20, backgroundColor: '#ccc'}}
                                    onPress={() =>this.handlepatch()  }>
                    <Text>PATCH</Text>
                </TouchableHighlight>
                <TouchableHighlight style={{padding: 20, backgroundColor: '#ccc'}}
                                    onPress={() =>this.handlerUploadWith({'phone_number': '12312312345', 'password': '666666', 'appkey': '18d3f1ac873e0','name': "hahah",'code':'123123'})}>
                    <Text>Upload</Text>
                </TouchableHighlight>

            </View>
        );
    }
}
var styles = StyleSheet.create({
    container: {
        //   justifyContent: 'center',
        flex: 1,
        flexDirection: 'column',//没有flexDirection这个属性的话，justifyContent不会生效
        justifyContent: 'center',
        padding: 15,
        alignItems: 'center'
    }
});
AppRegistry.registerComponent('TestNetWorkManager', () => TestNetWorkManager);